{
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# Welcome to Slim Extract Fast Start by LLMWare.ai 🦾\n",
        "This is the 3rd tutorial of our Agents Fast Start Series. In this tutorial, we will dive into the use of Sliim Extract model.\n",
        "\n",
        "\n",
        "## Slim Extract Model ...\n",
        "\n",
        "SLIMs are **Structured Language Instruction Models**, which are small, specialized 1-3B parameter LLMs, finetuned to generate structured outputs (Python dictionaries and lists, JSON and SQL) that can be handled programmatically, and stacked together in multi-step, multi-model Agent workflows - all running on a local CPU.\n",
        "\n",
        "The Slim-extract model are used to **extract custom keys from selected text**.\n"
      ],
      "metadata": {
        "id": "7nv_BZnr7MUU"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# install dependencies\n",
        "!pip3 install llmware # if you're getting error, then try upgrading the pip by runnnig this command : python -m pip install --upgrade pip"
      ],
      "metadata": {
        "id": "K68hEcgu78my"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "If you have any dependency install issues, please review the README, docs link, or raise an Issue.\n",
        "\n",
        "Usually, if there is a missing dependency, the code will give the warning - and a clear direction like `pip install transformers'` required for this example, etc.\n",
        "\n",
        "As an alternative to pip install ... if you prefer, you can also clone the repo from github which provides a benefit of having access to 100+ examples.\n",
        "\n",
        "To clone the repo:\n",
        "```\n",
        "git clone \"https://www.github.com/llmware-ai/llmware.git\"\n",
        "sh \"welcome_to_llmware.sh\"\n",
        "```\n",
        "\n",
        "The second script `\"welcome_to_llmware.sh\"` will install all of the dependencies.\n",
        "\n",
        "If using Windows, then use the `\"welcome_to_llmware_windows.sh\"` script."
      ],
      "metadata": {
        "id": "TjpSZbx179pB"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Import the ModelCatalog\n",
        "from llmware.models import ModelCatalog"
      ],
      "metadata": {
        "id": "DFpu9BOd8Ax6"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "We have included a set of sample earnings releases (comprising lines ~10 - ~385 of this script), and run a simple loop through the earnings releases, showing how to create an extract prompt to identify 'revenue growth' from these examples.\n",
        "\n",
        "here are several function-calling models in the slim-extract family, fine-tuned on multiple leading small model base foundations - full list and options are below in the code."
      ],
      "metadata": {
        "id": "7U3N9zlV8PkI"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "earnings_releases = [\n",
        "\n",
        " {\"context\": \"Adobe shares tumbled as much as 11% in extended trading Thursday after the design software maker \"\n",
        "    \"issued strong fiscal first-quarter results but came up slightly short on quarterly revenue guidance. \"\n",
        "    \"Here’s how the company did, compared with estimates from analysts polled by LSEG, formerly known as Refinitiv: \"\n",
        "    \"Earnings per share: $4.48 adjusted vs. $4.38 expected Revenue: $5.18 billion vs. $5.14 billion expected \"\n",
        "    \"Adobe’s revenue grew 11% year over year in the quarter, which ended March 1, according to a statement. \"\n",
        "    \"Net income decreased to $620 million, or $1.36 per share, from $1.25 billion, or $2.71 per share, \"\n",
        "    \"in the same quarter a year ago. During the quarter, Adobe abandoned its $20 billion acquisition of \"\n",
        "    \"design software startup Figma after U.K. regulators found competitive concerns. The company paid \"\n",
        "    \"Figma a $1 billion termination fee.\"},\n",
        "\n",
        " {\"context\": \"Dick’s Sporting Goods raised its dividend by 10% on Thursday as the company posted its largest sales \"\n",
        "    \"quarter in its history and projected another year of growth. The company’s shares jumped more than \"\n",
        "    \"15% in intraday trading. CEO Lauren Hobart said on an earnings call Thursday that Dick’s sales \"\n",
        "    \"growth came from bigger tickets — either higher prices or more expensive items — as its transactions \"\n",
        "    \"were flat. Many retailers benefited from a 53rd week in fiscal 2023, but Dick’s said it still broke \"\n",
        "    \"records during its fiscal fourth quarter even without those extra days. Here’s how the athletic \"\n",
        "    \"apparel retailer did compared with what Wall Street was anticipating, based on a survey of \"\n",
        "    \"analysts by LSEG, formerly known as Refinitiv: Earnings per share: $3.85 adjusted vs. $3.35 expected \"\n",
        "    \"Revenue: $3.88 billion vs. $3.80 billion expected The company’s reported net income for the three-month \"\n",
        "    \"period that ended Feb. 3 was $296 million, or $3.57 per share, compared with $236 million, or $2.60 a \"\n",
        "    \"share, a year earlier. Excluding one-time items related to impairment charges and inventory write-offs, \"\n",
        "    \"Dick’s reported earnings per share of $3.85. Sales rose to $3.88 billion, up about 8% from $3.60 billion \"\n",
        "    \"a year earlier. “With our industry-leading assortment and strong execution, we capped off the year \"\n",
        "    \"with an incredibly strong fourth quarter and holiday season,” Hobart said in a statement. “We are \"\n",
        "    \"guiding to another strong year in 2024. We plan to grow both our sales and earnings through \"\n",
        "    \"positive comps, higher merchandise margin and productivity gains,” she added. During the quarter, \"\n",
        "    \"same-store sales rose 2.8%, well ahead of the 0.8% lift that analysts had expected, according to \"\n",
        "    \"StreetAccount. “Growth in transactions” and market share gains drove the increase, said Executive \"\n",
        "    \"Chairman Ed Stack.\"},\n",
        "\n",
        " {\"context\": \"Comcast topped both revenue and profit estimates in the fourth quarter as it lost fewer broadband \"\n",
        "    \"subscribers than expected, and it raised its dividend 7%, the company said Thursday. \"\n",
        "   \"Here’s how Comcast performed, compared with estimates from analysts surveyed by LSEG, \"\n",
        "   \"formerly known as Refinitiv.  Earnings per share: 84 cents adjusted vs. 79 cents expected  \"\n",
        "   \"Revenue: $31.25 billion vs. $30.51 billion expected For the quarter ended Dec. 31, net \"\n",
        "   \"income rose 7.8% to $3.26 billion, or 81 cents a share, compared with $3.02 billion, or \"\n",
        "   \"70 cents a share, a year earlier. Revenue increased 2.3% compared with the prior-year period. \"\n",
        "   \"Adjusted earnings before interest, taxes, depreciation and amortization (EBITDA) was flat year \"\n",
        "   \"over year at about $8 billion.   'For the third consecutive year, we generated the highest revenue, \"\n",
        "   \"adjusted EBITDA and adjusted EPS in our company’s history', Comcast Chief Executive Officer Brian \"\n",
        "   \"Roberts said in a statement. 'We also reported the highest adjusted EBITDA on record at Theme Parks; \"\n",
        "   \"were the #1 studio in worldwide box office for the first time since 2015; and maintained Peacock’s \"\n",
        "   \"position as the fastest growing streamer in the U.S.'\"},\n",
        "\n",
        " {\"context\": \"Dollar General forecast annual sales above Wall Street estimates on Thursday, banking on higher \"\n",
        "     \"demand from inflation-hit customers buying groceries and essentials from the discount retailer’s stores.  \"\n",
        "     \"Shares of the company rose about 6% in early trading, after falling nearly 45% in 2023 on rising costs \"\n",
        "     \"and stiff competition from bigger retailers. But higher prices and borrowing costs have prompted \"\n",
        "     \"budget-conscious consumers to cook more meals at home, helping Dollar General record stronger \"\n",
        "     \"footfall at its outlets as shoppers hunt for lower-margin, needs-based goods, over pricier general \"\n",
        "     \"merchandise. “With customer traffic growth and market share gains during the quarter, we believe our \"\n",
        "     \"actions are resonating with customers,” CEO Todd Vasos said in a statement. Vasos’s strategy - to focus \"\n",
        "     \"on the basics, like more employee presence at stores, greater customer engagement and expanding \"\n",
        "     \"private-label brands - has helped stabilize Dollar General’s business. Over the last few quarters, \"\n",
        "     \"Dollar General and rival Dollar Tree have struggled with rising costs linked to their supply \"\n",
        "     \"chains, labor and raw materials, while facing tough competition from retailers like Walmart \"\n",
        "     \"and Chinese ecommerce platform Temu. Dollar Tree’s shares fell more than 15% on Wednesday, after it \"\n",
        "     \"forecast weak sales and profit for 2024 and laid out plans to shutter 970 of its Family Dollar \"\n",
        "     \"stores. “Dollar General has a much rosier outlook than Dollar Tree... Dollar Tree’s challenges \"\n",
        "     \"with Family Dollar were years in the making, while Dollar General has embarked on an aggressive \"\n",
        "     \"effort to add more frozen, refrigerated and fresh produce,” eMarketer senior analyst Zak Stambor said.  \"\n",
        "     \"Dollar General forecast 2024 sales to grow between 6.0% and 6.7%, above analysts’ estimate of 4.4% \"\n",
        "     \"growth to $40.33 billion, according to LSEG data. It still sees annual per-share profit between \"\n",
        "     \"$6.80 and $7.55, compared with estimates of $7.55.  Its fourth-quarter net sales of $9.86 billion \"\n",
        "     \"surpassed estimates of $9.78 billion. It also reported an estimate-beating profit of $1.83 per share.\"},\n",
        "\n",
        " {\"context\":  \"Best Buy surpassed Wall Street’s revenue and earnings expectations for the holiday quarter on \"\n",
        "                 \"Thursday, even as the company navigated through a period of tepid consumer electronics demand.  \"\n",
        "                 \"But the retailer warned of another year of softer sales and said it would lay off workers and \"\n",
        "                 \"cut other costs across the business. CEO Corie Barry offered few specifics, but said the \"\n",
        "                 \"company has to make sure its workforce and stores match customers’ changing shopping habits. \"\n",
        "                 \"Cuts will free up capital to invest back into the business and in newer areas, such as artificial \"\n",
        "                 \"intelligence, she added. “This is giving us some of that space to be able to reinvest into \"\n",
        "                 \"our future and make sure we feel like we are really well positioned for the industry to \"\n",
        "                 \"start to rebound,” she said on a call with reporters. For this fiscal year, Best Buy anticipates \"\n",
        "                 \"revenue will range from $41.3 billion to $42.6 billion. That would mark a drop from the most \"\n",
        "                 \"recently ended fiscal year, when full-year revenue totaled $43.45 billion. It said comparable \"\n",
        "                 \"sales will range from flat to a 3% decline. The retailer plans to close 10 to 15 stores \"\n",
        "                 \"this year after shuttering 24 in the past fiscal year. One challenge that will affect sales \"\n",
        "                 \"in the year ahead: it is a week shorter. Best Buy said the extra week in the past fiscal \"\n",
        "                 \"year lifted revenue by about $735 million and boosted diluted earnings per share by about \"\n",
        "                 \"30 cents. Shares of Best Buy closed more than 1% higher Thursday after briefly touching \"\n",
        "                 \"a 52-week high of $86.11 earlier in the session. Here’s what the consumer electronics \"\n",
        "                 \"retailer reported for its fiscal fourth quarter of 2024 compared with what Wall Street was \"\n",
        "                 \"expecting, based on a survey of analysts by LSEG, formerly known as Refinitiv: \"\n",
        "                 \"Earnings per share: $2.72, adjusted vs. $2.52 expected Revenue: $14.65 billion vs. $14.56 \"\n",
        "                 \"billion expected A dip in demand, but a better-than-feared holiday Best Buy has dealt \"\n",
        "                 \"with slower demand in part due to the strength of its sales during the pandemic. Like \"\n",
        "                 \"home improvement companies, Best Buy saw outsized spending as shoppers were stuck at \"\n",
        "                 \"home. Plus, many items that the retailer sells like laptops, refrigerators and home \"\n",
        "                 \"theater systems tend to be pricier and less frequent purchases. The retailer has cited other \"\n",
        "                 \"challenges, too: Shoppers have been choosier about making big purchases while dealing \"\n",
        "                 \"with inflation-driven higher prices of food and more. Plus, they’ve returned to \"\n",
        "                 \"splitting their dollars between services and goods after pandemic years of little \"\n",
        "                 \"activity. Even so, Best Buy put up a holiday quarter that was better than feared. \"\n",
        "                 \"In the three-month period that ended Feb. 3, the company’s net income fell by 7% to \"\n",
        "                 \"$460 million, or $2.12 per share, from $495 million, or $2.23 per share in the year-ago \"\n",
        "                 \"period. Revenue dropped from $14.74 billion a year earlier. Comparable sales, a metric that \"\n",
        "                 \"includes sales online and at stores open at least 14 months, declined 4.8% during the \"\n",
        "                 \"quarter as shoppers bought fewer appliances, mobile phones, tablets and home theater \"\n",
        "                 \"setups than the year-ago period. Gaming, on the other hand, was a strong sales \"\n",
        "                 \"category in the holiday quarter.\"}\n",
        "\n",
        "]"
      ],
      "metadata": {
        "id": "I49eI2UI8RaX"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Available Slim Extract Models\n",
        "slim_extract_models = [\"slim-extract-tool\",                 #   original - stablelm-3b (2.7b)\n",
        "                       \"slim-extract-tiny-tool\",            #   tiny-llama 1.1b\n",
        "                       \"slim-extract-qwen-1.5b-gguf\",       #   **NEW** qwen 1.5b\n",
        "                       \"slim-extract-phi-3-gguf\",           #   **NEW** phi-3 (3.8b)\n",
        "                       \"slim-extract-qwen-0.5b-gguf\"]       #   **NEW** qwen 0.5b"
      ],
      "metadata": {
        "id": "wbtHQKMP8rDY"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Load the Model\n",
        "model = ModelCatalog().load_model(\"slim-extract-tool\",sample=False,temperature=0.0, max_output=100) # You can change the model from any of the above one and test it yourself."
      ],
      "metadata": {
        "id": "5STiKpjk83_s"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "#   iterate through the earnings release samples above\n",
        "for i, sample in enumerate(earnings_releases):\n",
        "\n",
        "    #   key line: execute function_call on selected model with 'custom_key' = \"revenue growth\"\n",
        "    response = model.function_call(sample[\"context\"], function=\"extract\", params=[\"revenue growth\"])\n",
        "\n",
        "    #   display the response on the screen\n",
        "    print(\"extract response: \", i, response[\"llm_response\"])"
      ],
      "metadata": {
        "id": "SW_J08CB88LC"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Final Notes 🐛\n",
        "SLIM or Structured Language Instruction Models are powerful small models and have a great use case.\n",
        "\n",
        "In this fast start series, we experimented with the SLIM-EXTRACT MODEL that has a great use case in extracting custom keys from selected text.\n",
        "\n",
        "The available SLIM EXTRACT MODELS are:\n",
        "- slim-extract-tool [original - stablelm-3b (2.7b)]\n",
        "- slim-extract-tiny-tool [tiny-llama 1.1b]\n",
        "- slim-extract-qwen-1.5b-gguf [**NEW** qwen 1.5b]\n",
        "- slim-extract-phi-3-gguf [**NEW** phi-3 (3.8b)]\n",
        "- slim-extract-qwen-0.5b-gguf [**NEW** qwen 0.5b]\n",
        "\n",
        "HomeWork:\n",
        "Test all these on your own with your cutsom test cases!"
      ],
      "metadata": {
        "id": "lXq7-toh9jwR"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Journey is yet to start!\n",
        "Loved it?? This is just an example of our models. Please check out our other Agentic AI examples with every model in detail here: https://github.com/llmware-ai/llmware/tree/main/fast_start/agents\n",
        "\n",
        "Also, if you have more interest in RAG, then please go with our RAG examples, which you can find here: https://github.com/llmware-ai/llmware/tree/main/fast_start/rag\n",
        "\n",
        "If you liked it, then please **star our repo https://github.com/llmware-ai/llmware** ⭐\n",
        "\n",
        "Any doubts?? Join our **discord server: https://discord.gg/GN49aWx2H3** 🫂"
      ],
      "metadata": {
        "id": "HoVSbPiX9bwG"
      }
    }
  ],
  "metadata": {
    "colab": {
      "gpuType": "T4",
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}